import { defineStore } from 'pinia'
import { setItem, getItem } from '@/libs/utils/storage'
import { TAGS_VIEW } from '@/libs/utils/constant'
interface AppStore {
  showChangePassword: boolean
  tagsViewList: [] | any
  tagPath: string[]
  openKeys: string[]
}

interface addTagsType {
  fullPath: any
  meta: any
  name: any
  params: any
  path: any
  query: any
  title: any
}

export const useAppStore = defineStore({
  id: 'useAppStore',
  state: (): AppStore => ({
    showChangePassword: false, //忘记密码模态框
    tagsViewList: getItem(TAGS_VIEW) || [],
    tagPath: ['/home'],
    openKeys: ['/home']
  }),
  actions: {
    toogleShowChangePassword() {
      this.showChangePassword = !this.showChangePassword
    },
    //   添加tags
    addTagsViewList(tag: addTagsType) {
      const isFind = this.tagsViewList.find((item: addTagsType) => {
        return item.path === tag.path
      })
      if (!isFind) {
        this.tagsViewList.push(tag)
        setItem(TAGS_VIEW, this.tagsViewList)
      }
    },
    removeTagsViewList() {
      this.tagsViewList = []
    },
    /**
     *
     * @param {*} state
     * @param {*} payload .type 删除 可能为右侧 或者其他 或者自身 "right" || "other" || "index"
     */
    removeTagsView(payload: any) {
      if (payload.type === 'index') {
        this.tagsViewList.splice(payload.index, 1)
      } else if (payload.type === 'other') {
        this.tagsViewList.splice(payload.index + 1, this.tagsViewList.length - payload.index + 1)
        this.tagsViewList.splice(1, payload.index - 1)
      } else if (payload.type === 'right') {
        this.tagsViewList.splice(payload.index + 1, this.tagsViewList.length - payload.index + 1)
      } else if (payload.type === 'left') {
        this.tagsViewList.splice(0, payload.index)
      }
      setItem(TAGS_VIEW, this.tagsViewList)
    },
    updateTagPath(path: string[]) {
      this.tagPath = path
    },
    updateOpenKeys(path: string[]) {
      this.openKeys = path
    },
    updateTagsViewList(tagsViewList: string[]) {
      this.tagsViewList = tagsViewList
      setItem(TAGS_VIEW, this.tagsViewList)
    }
  },
  getters: {
    isShowChangePassword(state) {
      return state.showChangePassword
    },
    tagsViewLists: (state) => state.tagsViewList
  }
})
